package com.cskaoyan.controller;

import com.cskaoyan.annotation.AdminLog;
import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.basebean.BaseRespVo;
import com.cskaoyan.bean.zhao_order_bean.BasePageData;
import com.cskaoyan.bean.zhao_order_bean.ChannelVO;
import com.cskaoyan.bean.zhao_order_bean.DetailBasePageData;
import com.cskaoyan.service.OrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

/**
 * <h3>market</h3>
 *
 * @author:Zhao
 * @date:2021-10-18 17:50:07
 * @description: 请求参数
 * page: 1
 * limit: 20
 * sort: add_time //按添加时间排序
 * order: desc
 * start:
 * end:
 * <p>
 * 响应参数
 * data: {total: 18, pages: 1, limit: 20, page: 1, list: [,…]}
 * <p>
 * limit: 20
 * list: [,…]
 * page: 1
 * pages: 1
 * total: 18
 * errmsg: "成功"
 * errno: 0
 */
@RestController
@RequestMapping("admin/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @RequiresPermissions(value = "admin:order:list")
    @RequestMapping("list")
    public BaseRespVo list(Integer userId, String orderSn, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date start,
                           @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date end, BaseParam param) {


        BasePageData data = orderService.query(userId, orderSn, start, end, param);

        return BaseRespVo.ok(data);
    }


    @RequiresPermissions(value = "admin:order:read")
    @RequestMapping("detail")
    //参数为订单id，只要有值必会执行此方法
    public BaseRespVo detail(Integer id) {


        DetailBasePageData data = orderService.detail(id);

        return BaseRespVo.ok(data);
    }

    // 返回快递公司列表，不知道去哪个数据库取(写死？)
    @RequestMapping("channel")

    public BaseRespVo channel() {

//        ChannelVO channelVO = new ChannelVO();

        return BaseRespVo.ok(ChannelVO.getChannelList());
    }

    @AdminLog(type = 2, action = "发货")
    @RequiresPermissions(value = "admin:order:ship")
    @RequestMapping("ship")
    //订单发货,只需将订单状态改为已发货即可，并修改发货编号和发货快递公司
    //请求参数：
    //orderId: 15
    //shipChannel: "ZTO"
    //shipSn: "112233"
    //响应：
    //{"errno":0,"errmsg":"成功"}
    public BaseRespVo ship(@RequestBody Map map) {

        //获取请求参数
        Integer orderId = (Integer) map.get("orderId");
        String shipChannel = (String) map.get("shipChannel");
        String shipSn = (String) map.get("shipSn");

        orderService.ship(orderId, shipChannel, shipSn);
        return BaseRespVo.ok();
    }

    @AdminLog(type = 2, action = "退款")
    @RequiresPermissions("admin:order:refund")
    @RequestMapping("refund")
    //订单退款
    //请求参数
    //orderId: 6
    //refundMoney: 398
    public BaseRespVo refund(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        orderService.refund(orderId);

        return BaseRespVo.ok();
    }

    @AdminLog(type = 2, action = "删除")
    @RequiresPermissions(value = "admin:order:delete")
    @RequestMapping("delete")
    //删除
    public BaseRespVo delete(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        orderService.delete(orderId);

        return BaseRespVo.ok();
    }
}
